<?php
namespace Common\Controller;
use Think\Controller;
use Common\Controller\BaseController;

class AdminController extends BaseController {

    protected $where = [];
    protected $table = 'user';
    protected $info  = 'id,name';

    protected function _initialize()
    {
        parent::_initialize();

        if(!session("username")){
            //Header("Location:/home/login");
            echo '<h2 style="margin:100px 0 0 100px">登陆超时，<a target="_parent" href="/home/login">点击此处</a>重新登陆</h2>';
            exit;
        }

        //登陆用户信息
        $where = array("username"=>session("username"));
        $user = M("user u")
            ->field("u.*, r.name as role_name, r.code as role_code")
            ->where($where)
            ->join(C('DB_PREFIX')."role r on r.id=u.role_id", "LEFT")
            ->find();
        $this->assign('u', $user);
        self::$u = $user;

        //权限代码
        $rightsCode = M("rassign a")
            ->field("r.code")
            ->where(array('a.role_id'=>$user['role_id']))
            ->join(C('DB_PREFIX')."rights r on r.id=a.rights_id", "LEFT")
            ->select();
        $code = array();
        foreach ($rightsCode as $item) {
            $code[] = $item['code'];
        }
        $this->assign('rightsCode', $code);
    }

    public function delete($where = []){
        if($where != []){
            $this->where = $where;
        }
        if($this->where != []){
            $result = M($this->table)->where($this->where)->save(['status' => 0]);
            return $result;
        }else{
            return false;
        }
    }

}